import Vue from 'vue'
import Vuex from 'vuex'
import { getSlideList, getExhibitionList, getNewsList, getExhibitionListFilterOptions } from '@/api'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    slideList: [],
    homeHotExhibitionList: [],
    homeNewsList: [],
    exhibitionListFilterOptions: []
  },
  mutations: {
    setSlideList (state, payload) {
      state.slideList = payload
    },
    setHomeHotExhibitionList (state, payload) {
      state.homeHotExhibitionList = payload
    },
    setHomeNewsList (state, payload) {
      state.homeNewsList = payload
    },
    setExhibitionListFilterOptions (state, payload) {
      state.exhibitionListFilterOptions = payload
    }
  },
  actions: {
    async getSlideList ({ commit }) {
      const { code, data } = await getSlideList()
      if (code === 1) {
        let rawData = data
        rawData = rawData.map((e) => {
          return {
            title: e.title,
            image: e.logo,
            href: e.href
          }
        })
        commit('setSlideList', rawData)
      }
    },
    async getHomeHotExhibitionList ({ commit }) {
      const { code, data } = await getExhibitionList({
        params: {
          type: 1,
          page: 1,
          pageSize: 4
        }
      })
      if (code === 1) {
        let rawData = data.list
        rawData = rawData.map((e) => {
          return {
            id: e.ex_id,
            title: e.ex_name,
            image: e.ex_logo,
            date: e.ex_time
          }
        })
        commit('setHomeHotExhibitionList', rawData)
      }
    },
    async getHomeNewsList ({ commit }) {
      const { code, data } = await getNewsList({
        params: {
          page: 1,
          pageSize: 4
        }
      })
      if (code === 1) {
        let rawData = data.list
        rawData = rawData.map((e) => {
          return {
            id: e.news_id,
            title: e.news_title,
            image: e.news_logo,
            date: e.news_time
          }
        })
        commit('setHomeNewsList', rawData)
      }
    },
    async getExhibitionListFilterOptions ({ commit }) {
      const { code, data } = await getExhibitionListFilterOptions()
      if (code === 1) {
        const out = []
        let options

        options = data.industries.map((e) => {
          return {
            label: e.name,
            value: e.id
          }
        })
        out.push({
          label: '按行业',
          options
        })

        options = []
        for (const key in data.district) {
          options.push({
            label: key,
            readonly: true
          })
          for (const item of data.district[key]) {
            options.push({
              label: item.name,
              value: item.id
            })
          }
        }
        out.push({
          label: '按地区',
          options
        })

        options = data.date.map((e) => {
          return {
            label: e.format,
            value: e.range
          }
        })
        out.push({
          label: '按时间',
          options
        })

        commit('setExhibitionListFilterOptions', out)
      }
    }
  }
})
